<?php

class OPPlayer {
	/**
	* -------------------------------------------------------------------------
	*
	* 									Attributes
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	* Players database
	*/
	protected static $database;
	
	/**
	 * Identifier of the player
	 * 
	 * @var String
	 */
	protected $id = null;
	
	/**
	* Position of the player
	*
	* @var OPPosition
	*/
	protected $position;
	
	/**
	 * -------------------------------------------------------------------------
	 * 
	 * 									Static
	 * 
	 * -------------------------------------------------------------------------
	 */
	
	/**
	 * Static constructor creating a new Player with an id
	 * 
	 * @param $id
	 * @return OPPlayer
	 */
	public static function newWithId($id){
		$newPlayer = new OPPlayer();
		$newPlayer->setId($id);
		
		return $newPlayer;
		
	}
	
	/**
	* Static constructor creating a new Player with a position
	*
	* @param $position
	* @return OPPlayer
	*/
	public static function newWithPosition($position){
		$newPlayer = new OPPlayer();
		$newPlayer->setPosition($position);
	
		return $newPlayer;
	
	}
	
	/**
	* Static constructor creating a new Player with an id and position
	*
	* @param $id, $position
	* @return OPPlayer
	*/
	public static function newWithIdAndPosition($id, $position){
		$newPlayer = OPPlayer::newWithId($id);
		$newPlayer->setPosition($position);
	
		return $newPlayer;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Getters
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Return players database
	 */
	public static function database() {		
		return OPPlayer::$database;
		
	}
	
	/**
	 * Return all the players in the database
	 */
	public static function allPlayers() {
		$allPlayers = null;
		if (null == OPPlayer::database()) {
			$allPlayers = OPDatabase::allPlayers();
			
		}else {
			$allPlayers = OPPlayer::database()->allPlayers();
							
			
		}
		
		return $allPlayers;
		
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Setters
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Set players database
	 * 
	 * @param $database
	 */
	public static function setDatabase($database) {
		OPPlayer::$database = $database;
		
	}
	
	/**
	 * Get the number of players arround a position
	 * 
	 * @param $position
	 * @return int
	 */
	public static function playersArround($position) {
		$allPlayers = OPPlayer::allPlayers();
		$playersArround = null;
		
		if ($allPlayers != null) {
			foreach ($allPlayers as $aPlayer) {
				if(OPPosition::distanceBetween($aPlayer->position(), $position) <= 20) {
					$playersArround[] = $aPlayer;
					
				}
				
			}
		
		}
		
		return $playersArround;
		
	}
	
	/**
	* -------------------------------------------------------------------------
	*
	* 									Dynamic
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	* -------------------------------------------------------------------------
	* 									Setters
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Set the $id to the player
	 * 
	 * @param $id
	 * @return OPPlayer
	 */
	public function setId($id) {
		$this->id = $id;
		
		return $this;
		
	}
	
	/**
	* Set player's position
	*
	* @param $position
	* @return OPPLayer
	*/
	public function setPosition($position) {
		$this->position = $position;
	
		return $this;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Getters
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Return the id of the player
	 *
	 * @return String
	 */
	public function id() {
		return $this->id;
	
	}
	
	/**
	* Return the position of the player
	*
	* @return OPPosition
	*/
	public function position() {
		return $this->position;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									OPIDatabase
	* -------------------------------------------------------------------------
	*/
	
	public function save() {
		OPDatabase::savePlayer($this);
	
	}
	
}